﻿package com.view 
{
	import com.Model.WomanProxy;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import fl.controls.Slider;
	import fl.events.SliderEvent;
	
	/**
	 * ...
	 * @author zheng sir
	 */
	public class PaintMenuMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "paintMenuMediator_name";
		public static const HAIRCHANGED:String = "paintMenuMediator_hairChanged";//换头发
		public static const COLORCHANGED:String = "paintMenuMediator_colorChanged";//中间被点
		//public static const SLIDERCHANGED:String = "paintMenuMediator_sliderChanged";//滑块改变
		
		private static const FIRSTNUM:int = 1;//mid第一次显示哪个？
		private static const MIDTOTALNUM:int = 5;//mid总数
		private static const BOTTOMTOTALNUM:int = 5;//bottom总数
		
		public function PaintMenuMediator(mediatorName:String = null, viewComponent:Object = null) 
		{
			super(mediatorName, viewComponent);
			
		}
		override public function onRegister():void 
		{
			super.onRegister();
			setNum();
			addEventSlider();
			initShow(FIRSTNUM);
		}
		/**
		 * 设置每个元件的num值
		 */
		private function setNum():void {
			for (var i = 1; i <= MIDTOTALNUM; i++ ) {
				var mc:MovieClip = viewComponent.paintMenuMc["top" + i] as MovieClip;
				mc.num = i;
				mc.addEventListener(MouseEvent.CLICK, topEventHandler);
				addBtnEvent(mc);
				var childrenNum:int = MovieClip(viewComponent.paintMenuMc["mid" + i]).numChildren
				var midMc:MovieClip = MovieClip(viewComponent.paintMenuMc["mid" + i]);
				midMc.num = i;
				for (var j = 1; j <= childrenNum - 1; j++ ) {
					midMc["a" + j].num = j;
					midMc["a" + j].addEventListener(MouseEvent.CLICK, midEventHandler);
					addBtnEvent(midMc["a" + j]);
				}
			}
			for (var k = 1; k <= MIDTOTALNUM; k++ ) {
				var bottomMc:MovieClip = viewComponent.paintMenuMc["bottom" + k] as MovieClip;
				bottomMc.num = k
				bottomMc.addEventListener(MouseEvent.CLICK, bottomEventHandler);
				addBtnEvent(bottomMc);
			}
		}
		/**
		 * 添加滑动器的事件
		 */
		private function addEventSlider():void {
			var slider:Slider = viewComponent.paintMenuMc.slider as Slider;
			slider.minimum = 1;
			slider.maximum = 100;
			slider.addEventListener(SliderEvent.THUMB_DRAG, onThumbDrag);
		}
		
		private function onThumbDrag(e:SliderEvent):void 
		{
			var slider:Slider = e.currentTarget as Slider;
			var womanProxy:WomanProxy = facade.retrieveProxy(WomanProxy.NAME) as WomanProxy;
			womanProxy.sliderNum = slider.value;
		}
		/**
		 * 显示那一排的笔刷
		 */
		private function initShow(num:int):void {
			for (var i = 1; i <= MIDTOTALNUM; i++ ) {
				var mc:MovieClip = viewComponent.paintMenuMc["mid" + i] as MovieClip;
				if (i != num) {
					mc.alpha = .5;
					mc.mouseEnabled = false;
					mc.mouseChildren = false;
				}else {
					mc.alpha = 1;
					mc.mouseEnabled = true;
					mc.mouseChildren = true;
				}
			}
		}
		
		private function topEventHandler(e:MouseEvent):void 
		{
			var mc:MovieClip = e.currentTarget as MovieClip;
			viewComponent.paintMenuMc.outNum = mc.num;
			viewComponent.paintMenuMc.inNum=1;//每次点击top按钮，初始化
			sendNotification(MouseMediator.MOUSECHANGED, mc.num);
			initShow(mc.num);
		}
		private function bottomEventHandler(e:MouseEvent):void 
		{
			var mc:MovieClip = e.currentTarget as MovieClip;
			sendNotification(HAIRCHANGED, mc.num);
		}
		
		private function midEventHandler(e:MouseEvent):void 
		{
			var inMc:MovieClip = e.target as MovieClip;
			viewComponent.paintMenuMc.inNum = inMc.num;
			
			sendNotification(MouseMediator.CHANGEDNUM, inMc.num);
		}
		private function addBtnEvent(mc:MovieClip):void {
			mc.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			mc.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			mc.addEventListener(MouseEvent.CLICK, onClick);
		}
		private function delBtnEvent(mc:MovieClip):void {
			mc.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
			mc.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
			mc.removeEventListener(MouseEvent.CLICK, onClick);
		}
		private function onRollOver(e:MouseEvent ):void {
			var btnMusic:MovieClip = facade.retrieveMediator(ApplicationMediator.NAME).getViewComponent().btnMusic as MovieClip;
			btnMusic.gotoAndPlay(2);
			MovieClip(e.currentTarget).scaleX = ApplicationMediator.SCALEXY;
			MovieClip(e.currentTarget).scaleY = ApplicationMediator.SCALEXY;
		}
		private function onRollOut(e:MouseEvent):void 
		{
			MovieClip(e.currentTarget).scaleX = 1;
			MovieClip(e.currentTarget).scaleY = 1;
		}
		private function onClick(e:MouseEvent):void {
			var btnMusic:MovieClip = facade.retrieveMediator(ApplicationMediator.NAME).getViewComponent().btnMusic as MovieClip;
			btnMusic.gotoAndPlay(4);
		}
		
	}

}